#include "qserialproto.h"
#include "src/globalvar.h"

QSerialProto::QSerialProto(QObject *parent) : QObject(parent){
    this->mState = StateNOPACKAGE;
    this->mUnsortLen = 0;
    this->mRecvDataLen = 0;
}

void QSerialProto::OnSerailData(QByteArray data){
    qDebug()<<"receive data"<<data.length();
    if(this->mState == StateNOPACKAGE){
        uint8_t len = data.at(1);
        if(len > 255){
            qDebug()<<"len error";
        }
        if(len == data.length()){
            uint8_t verify = 0;
            for(uint16_t i = 0;i < data.length() - 1;i++){
                verify += data.at(i);
            }
            if(verify == data[data.length() - 1]){
                ProtoData ret;
                ret.len = data.at(1);
                ret.funcode = data.at(0);
                ret.data = new char[data.length() - 2];
                memcpy(ret.data,data.data() + 2,data.length() - 2);
                qDebug()<<"on package ";
                emit this->OnPackage(ret);
                return;
            }else{
                qDebug()<<"verify fail";
                return;
            }
        }
        if(len < data.length()) {
            mUnsortLen = len;
            this->mRecvDataLen += data.length();
            memcpy(mUnsortData,data.data(),data.length());

        }
        if(data.length() >  len){

        }
    }else{
        this->mRecvDataLen += data.length();
        memcpy(mUnsortData,data.data(),data.length());
        if(this->mRecvDataLen == mUnsortData[1]){  // 一个完整的包
            uint8_t verify = 0;
            for(uint16_t i = 0;i < mRecvDataLen;i++){
                verify += data.at(i);
            }
            if(verify == mUnsortLen){
                ProtoData ret;
                ret.len = mUnsortData[1];
                ret.funcode = mUnsortData[0];
                ret.data = new char[mRecvDataLen - 2];
                memcpy(ret.data,mUnsortData + 2,mRecvDataLen - 2);
                // 更新状态
                this->UpdateState(ANALYSEFINISH);
                emit this->OnPackage(ret);
                qDebug()<<"on package ";

            }else{
                qDebug()<<"verified ";
            }
        }
        if(this->mRecvDataLen < mUnsortData[1]){

        }
    }
    qDebug()<<"serial data"<<data.data();
}

int QSerialProto::UpdateState(AnalyseAction action){
    switch (action) {
    case INDATA:
        this->mState = StateINPACKAGE;
        break;
    case ANALYSEFINISH:
        this->mState = StateNOPACKAGE;
        this->mUnsortLen = 0;
        this->mRecvDataLen = 0;
        break;
    case ANALYSEINPROGRESS:
        this->mState = StateINPACKAGE;
    break;
    default:
        break;
    }
    return 0;
}

int QSerialProto::WriteData(uint8_t fun, uint8_t *data, uint8_t len){
    char verify = 0;
    char *send = new char[len + 3];
    send[0] = fun;
    send[1] = len;

    memcpy(send + 2,data,len - 2);
    for(uint8_t i = 0;i < len;i++){
        verify += send[i];
    }
    qDebug()<<QString("send verify is %1 %1 %1").arg(verify).arg(fun).arg(len);
    send[len ] = verify;
    return GlobalVarManager::Instance()->GetSerailController()->SendData((char*)send,len);
}
